﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalemShop
{
    public partial class FrmInvoice : Form
    {
        public FrmInvoice()
        {
            InitializeComponent();
            cbMaSo.PreviewKeyDown += new PreviewKeyDownEventHandler(cbMaSo_PreviewKeyDown);
        }

        void loadInvoiceIds()
        {
            cbMaSo.AutoCompleteCustomSource = Database.getInvoiceIds();
        }

        private void FrmInvoice_Load(object sender, EventArgs e)
        {
            loadInvoiceIds();
        }

        void cbMaSo_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                doAddInvoiceRow();
            }
        }

        void doAddInvoiceRow()
        {
            int maso;
            Int32.TryParse(cbMaSo.Text, out maso);
            DataTable dt = Database.getInvoiceBuying(maso);
            if (dt == null || dt.Rows.Count == 0)
            {
                bindingSource.DataSource = null;
                bindingSource.ResetBindings(true);
                lbDate.DataBindings.Clear();
                lbNhanVien.DataBindings.Clear();
                lbTotal.Text = "Tổng tiền: 0";
                return;
            }

            bindingSource.DataSource = dt;
            lbDate.DataBindings.Clear();
            lbDate.DataBindings.Add("Text", bindingSource, "invoiceDate");
            lbNhanVien.DataBindings.Clear();
            lbNhanVien.DataBindings.Add("Text", bindingSource, "seller");

            lbTotal.Text = "Tổng tiền: " + Database.getInvoiceBuyingTotalMoney(maso).ToString("#,0") + " VNĐ";
            bindingSource.ResetBindings(true);
        }

        private void FrmInvoice_FormClosing(object sender, FormClosingEventArgs e)
        {
            FrmMain frmMain = (FrmMain)this.MdiParent;
            frmMain.frmInvoice = null;
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            FrmLogin frmLogin = new FrmLogin();
            if (frmLogin.ShowDialog(this) == DialogResult.OK && frmLogin.loginOk && frmLogin.isAdmin)
            {
                FrmCreateInvoiceSp frm = new FrmCreateInvoiceSp();
                frm.ShowDialog(this);
            }
            else
            {
                FrmMain main = (FrmMain)this.MdiParent;
                main.doShowMessage("Thông báo", "Cần phải đăng nhập mới sử dụng được tính năng này.");
            }
        }

        private void btApply_Click(object sender, EventArgs e)
        {
            DataTable sr = Database.getInvoiceIds(dateTimePickerFrom.Value.ToString("yyyy-MM-dd"), dateTimePickerTo.Value.ToString("yyyy-MM-dd"));
            if (sr != null)
            {
                listInvoices.DataSource = sr;
                listInvoices.DisplayMember = "invoiceId";
                listInvoices.ValueMember = "invoiceId";
            }
            else
            {
                listInvoices.DataSource = null;
            }
        }


        private void listInvoices_DoubleClick(object sender, EventArgs e)
        {
            if (listInvoices.SelectedItem != null)
            {
                cbMaSo.Text = listInvoices.Text;
                doAddInvoiceRow();
            }
        }

        private void btCustomer_Click(object sender, EventArgs e)
        {
            if (listInvoices.SelectedValue == null)
            {
                return;
            }

            if (listInvoices.DataSource == null)
            {
                return;
            }

            lbCustomer.Text = String.Empty;
            FrmCheckPoint frm = new FrmCheckPoint();
            frm.StartPosition = FormStartPosition.CenterScreen;
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                Customer cs = frm.cs;
                if (cs != null)
                {
                    lbCustomer.Text = "Khách hàng : " + cs.ToString();
                    int invoiceId = 0;
                    Int32.TryParse(listInvoices.SelectedValue.ToString(), out invoiceId);
                    long point = Database.getInvoiceBuyingTotalMoney(invoiceId);
                    if (point > 0)
                    {
                        point = point / 1000;
                        if (!Database.updatePoint4Customer(cs, invoiceId, (int)point))
                        {
                            MessageBox.Show("Cập nhật điểm cho khách hàng thất bại, hóa đơn này đã được sử dụng");
                            lbCustomer.Text = String.Empty;
                        }
                    }
                }
            }
        }
    }
}
